package com.farmer.reliablemessage.core;


import com.farmer.reliablemessage.event.ReliableMessageSendContinueEvent;
import com.farmer.reliablemessage.event.ReliableMessageSendFailEvent;
import com.farmer.reliablemessage.event.ReliableMessageSendOkEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class EventScheduler {
    private ApplicationEventPublisher applicationEventPublisher;

    public EventScheduler(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Scheduled(
            initialDelay = 5000L,
            fixedDelay = 15000L
    )
    public void resend() {
        this.applicationEventPublisher.publishEvent(new ReliableMessageSendContinueEvent(this));
    }

    @Scheduled(
            initialDelay = 7000L,
            fixedDelay = 900000L
    )
    public void moveMessageToHis() {
        this.applicationEventPublisher.publishEvent(new ReliableMessageSendOkEvent(this));
    }

    @Scheduled(
            initialDelay = 13000L,
            fixedDelay = 1200000L
    )
    public void moveFailedMessage() {
        this.applicationEventPublisher.publishEvent(new ReliableMessageSendFailEvent(this));
    }
}
